<script setup lang="ts">
import BatchActionModal from "@cps/actionsNew/batchActionModalForFees.vue";
import BatchActionModalNew from "@cps/actionsNew/batchActionModalForFeesNew.vue";
import {ref} from "vue";
import {auth} from "@/utils/common";

const batchActionRef = ref()
const batchActionNewRef=ref()
const batch = ref({visible: false,})
const props = defineProps({
  buttonGroups: {type: Array, required: true},
  crudRef: {required: true},
  dataList:{required: true},
  currentStatus: {type: String, default: ''},
  auditView: {type: Boolean, default: false},
  selected: {required: true},
})
const isHideButton = (button) => {
  if (props.auditView) {
    return !button.AuditShow
  }
  if (!button.hideStatus) {
    return false
  }
  let list = typeof button.hideStatus != 'object' ? [button.hideStatus] : button.hideStatus
  return list.map(item => {
    return item !== null ? item.toString() : null;
  }).includes(props.currentStatus)
}
//按钮分组权限
const checkButton = (authArr) =>{
  for (let index = 0; index < authArr.length; index++) {
    const element = authArr[index];
    if(auth(element)){
        return true
    }
  }
}
</script>

<template>
  <template v-for="button in buttonGroups">
    <a-dropdown trigger="hover" :popup-max-height="false" v-if="button.children">
      <div v-show="!isHideButton(button)">
        <a-button style="margin:0px 10px 0 0;" size="small" type="primary" v-if="checkButton(button.auth?button.auth:[])">{{ button.title }}</a-button>
      </div>
      <template #content>
        <div v-for="buttonChild in button.children" v-show="!isHideButton(buttonChild)">
          <a-doption v-if="buttonChild.title=='按单添加应收'" @click="batchActionNewRef.open(buttonChild)" v-auth="buttonChild.auth?buttonChild.auth:[]">
            {{ buttonChild.title }}
          </a-doption>
          <a-doption v-else @click="batchActionRef.open(buttonChild)" v-auth="buttonChild.auth?buttonChild.auth:[]">
            {{ buttonChild.title }}
          </a-doption>
        </div>
      </template>
    </a-dropdown>
    <template v-else>
      <div v-show="!isHideButton(button)">
        <a-button style="margin:0px 10px 0 0;" type="primary" size="small"
                  @click="batchActionRef.open(button)"
                  v-auth="button.auth?button.auth:[]"
                  :status="button.status?button.status:'default'">
          {{ button.title.value ? button.title.value : button.title }}
        </a-button>
      </div>
    </template>
  </template>
  <batch-action-modal v-model="batch.visible"
                      :crud-ref="crudRef"
                      :dataList="dataList"
                      :selected="selected"
                      ref="batchActionRef"
  ></batch-action-modal>
  <batch-action-modal-new v-model="batch.visible"
                      :crud-ref="crudRef"
                      :dataList="dataList"
                      :selected="selected"
                      ref="batchActionNewRef"
  ></batch-action-modal-new>
</template>

<style scoped>

</style>